<?php

namespace App\Services;

use App\Http\Models\SmsLog;
use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\Exception;

class SmsService
{

    public static $config = [
        // HTTP 请求的超时时间（秒）
        'timeout' => 5.0,

        // 默认发送配置
        'default' => [
            // 网关调用策略，默认：顺序调用
            'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

            // 默认可用的发送网关
            'gateways' => [
                // 'aliyun',
                'yuntongxun'
            ],
        ],
        // 可用的网关配置
        'gateways' => [
            'errorlog' => [
                'file' => '/tmp/easy-sms.log',
            ],
            // 'aliyun' => [
            // 	'access_key_id' => 'LTAI5tGn9FJ4LYAzL3BaftP4',
            // 	'access_key_secret' => 'aS5AJU6ZZeGVq0el3ITjeFUX9U9g8P',
            // 	'sign_name' => '凯斯通',
            // ],
            'yuntongxun' => [
                'app_id'         => '2c94811c85c276590185fb76ae3403da', // 病例集ID
                'account_sid'    => '8aaf0708591604f301591742cc90010f',
                'account_token'  => 'd925c1d7247144cfb0e16c174f10d618',
                'is_sub_account' => false,
            ]
            //...
        ],
          'SMS' => [
            "IDCARD" => '1711078', // 身份验证验证码
            "LOGIN" => '1711078', // 登录确认验证码
            "REG" => '1711078', // 用户注册验证码
            "PWD" => '1711078', // 修改密码验证码
            "INFO" => '1711078', // 信息变更验证码
            "CURRENCY" => '1711078', //通用

        ],
    ];




    public static function  sendCode($phone, $code, $template)
    {

        $easySms = new EasySms(self::$config);
        try {
            SmsLog::create(
                [
                    'phone' => $phone,
                    'code'    => $code,
                    'add_ip' => request()->ip(),
                    'status'    => 0,
                    'add_time'    => date('Y-m-d H:i:s', time()),
                ]
            );
            $res = $easySms->send($phone, [
                // 'content'  => '您的验证码为: 6379',
                'template' => self::$config['SMS']['INFO'],
                'data' => [
                    // 'code' => $code
                    $code,5
                ],
            ]);
            return $res;
        } catch (Exception $e) {
            return [];
        }
    }
}
